package com.sdd.rpi_mobile.resourceLists;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.sdd.rpi_mobile.infoTypes.BuildingInfoActivity;
import com.sdd.rpi_mobile.infoTypes.InformationActivity;
import com.sdd.rpi_mobile.infoTypes.PlacesInfoActivity;

/** ListViewLoader for a list filled with items */
public class ListItemLoader implements ListViewLoader {
	Context mContext;
	
	public ListItemLoader(Context context) {
		this.mContext = context;
	}
	
	/** Returns an ArrayAdapter containing String array of names of the items */
	@Override
	public ArrayAdapter<String> getArrayAdapter(int resID) {	
		//Get unique id of string array
		final Resources res = mContext.getResources();
		
		TypedArray items = res.obtainTypedArray(resID);
		int length = items.length();
		
		String[] arrayAdapterItems = new String[length];
		
		for(int i = 0; i < length;i++){
			int listInfoID = items.getResourceId(i, 0);
			String[] listInfo = res.getStringArray(listInfoID);
			arrayAdapterItems[i] = listInfo[0];
		}
		items.recycle();
		
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.mContext, 
				android.R.layout.simple_list_item_1, arrayAdapterItems);
		return arrayAdapter;
	}

	/**
	 * Get intent with Bundle to show item
	 * @param resId
	 * Resource ID of array of items
	 * @param pos
	 * position of item 
	 */
	@Override
	public Intent getIntent(Context context, int resId, int pos) {
		Bundle bundle = new Bundle();
		Resources res = this.mContext.getResources();
		
		TypedArray items = res.obtainTypedArray(resId);
		
		int listInfoID = items.getResourceId(pos, 0);
		items.recycle();
		
		String[] object = res.getStringArray(listInfoID);
		Intent intent = null;
		if(object.length == 3){
			intent = new Intent(this.mContext, InformationActivity.class);
		}
		else if(object.length == 4) {
			intent = new Intent(this.mContext, BuildingInfoActivity.class);
		}
		else {
			intent = new Intent(this.mContext, PlacesInfoActivity.class);
		}
		
		bundle.putInt("resource", listInfoID);
		intent.putExtras(bundle);
		
		return intent;
	}
}